package Controllers;

import Behaviors.Arbitrators.Arbitrator;
import Robot.*;
import Robot.Robot;
import Behaviors.Schemas.*;
import Behaviors.Schemas.Motor.*;
import Behaviors.Schemas.Perception.*;
import Util.*;


public class BehaviorBasedController extends Controller implements Runnable{
    private Arbitrator arbitrator;
    private Robot robot;
    private String name;
    private int sleepTime;
    private boolean isThreaded = true;

    public BehaviorBasedController(String name, Robot robot, Arbitrator arbitrator,int sleepTimeMs) {
        super(name,robot);
        this.arbitrator=arbitrator;
        this.robot=robot;
        this.name=name;
        this.sleepTime =sleepTimeMs;



    }

    //defines wheter the controller will run as a thread or not
    public void runThreadedMode(boolean isThreaded)
    {
        this.isThreaded = isThreaded;
    }
    
    public void run(){
        
        if(isThreaded)
        {
            while(true)
            {
                 arbitrator.step();

                 if(isDebugging)
                     System.out.println(name);

                try{
                    //Thread.yield();
                    Thread.sleep(sleepTime);

                }

                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        
        else
        {
            arbitrator.step();
            if(isDebugging)
                System.out.println(name);
        }
          
    }
    
    public Arbitrator getArbitrator() {
        return arbitrator;
    }

    public void setArbitrator(Arbitrator val) {
        this.arbitrator = val;
    }
}
